package org.example.com.Demo;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class StringRedisDemo {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    // 存放
    public void a(){
        // string类型字符串进行存放操作
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        ops.set("A","aa");
        ops.set("B","bb");
        ops.set("C","cc");
        ops.setIfPresent("A","aa1");// 存在key 覆盖
        ops.setIfPresent("D","dd1");// 不存在key  不存放
        ops.setIfAbsent("B","bb2");// 存在key 不覆盖
        ops.setIfAbsent("E","ee2");// 不存在key 存放
        // set A aa
    }
    public void b(){
        ValueOperations<String,String> ops=stringRedisTemplate.opsForValue();
           String a=ops.get("A");
           String b=ops.get("B");
           String c=ops.get("C");
           String d=ops.get("D");
           String e=ops.get("E");
           log.info("a:{},b:{},c:{},d:{},e:{}",a,b,c,d,e);

    }
     public void c(){
        ValueOperations<String,String> ops=stringRedisTemplate.opsForValue();
         // 删除
         stringRedisTemplate.delete("A");
         // 追加
         ops.append("B", "abcd12344");
         // 自增
        ops.increment("D");//d+1
         ops.increment("D",3);//d+3
         ops.decrement("D");
         ops.decrement("D",2);



     }
    // 获取

}
